package cetcbigdata.da.zhengwugongkai;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SpiderHBZWGGFW extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private JSONObject parseListPage() throws IOException {

            int pages = 0;
            int currentPage = 1;
            //阐述分类遍历的原因：首先不同分类的页面结构和名称不一样，分开遍历利于分类出不同情况
            while (pages != currentPage) {
                System.out.println(currentPage);
                String listUrl = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdItemList.jspc";
                JSONObject parmas = new JSONObject();
                parmas.put("regionCode" , "420000000000");
                parmas.put("taskType","20");
                parmas.put("areaGrade" , "2");
                parmas.put("currentPage" , currentPage);
                parmas.put("pageSize" , "10");
                String listResponse = postFormReq(listUrl, new JSONObject(), parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);
                if (responseObj.getString("status").equals("200")) {
                    pages = responseObj.getJSONObject("data").getJSONObject("pageList").getInteger("pages");
                    JSONArray list = responseObj.getJSONObject("data").getJSONObject("pageList").getJSONArray("records");
                    for (Object o :list) {    //获取必要信息
                        String TASKCODE = ((JSONObject) o).getString("TASKCODE");
                        String TASKNAME = ((JSONObject) o).getString("TASKNAME");

                        //getQzqdSJYWList.jspc 访问该网页从而获取TRANSACTCODE
                        String listUrlnext = "http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdSJYWList.jspc";
                        JSONObject parmasnext = new JSONObject();
                        parmasnext.put("taskcode", TASKCODE);
                        String listResponsenext = postFormReq(listUrlnext, new JSONObject(), parmasnext);
                        JSONObject responseObjnext = JSONObject.parseObject(listResponsenext);
                        JSONArray transactCodeList = responseObjnext.getJSONObject("data").getJSONArray("records");
                        //oystem.out.println(transactCodeList);
                        for (Object o1 : transactCodeList) {
                            String TRANSACTCODE = ((JSONObject) o1).getString("TRANSACTCODE");

                            //开始解析详情页
                            JSONObject detailmessage = new JSONObject();

//                            String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
//                            JSONObject detailparmas1 = new JSONObject();
//                            detailparmas1.put("transactCode", TRANSACTCODE);
//                            detailparmas1.put("type", "legalbasis");
//                            String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas1);
//                            JSONObject content = this.parseDetailPage1(detailResponse);
//                            detailmessage.put("设定依据", content);
//
//
//                            JSONObject detailparmas2 = new JSONObject();
//                            detailparmas2.put("transactCode", TRANSACTCODE);
//                            detailparmas2.put("type", "handlingprocess");
//                            detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas2);
//                            content = this.parseDetailPage2(detailResponse);
//                            detailmessage.put("办理流程", content);
//
//                            JSONObject detailparmascondition = new JSONObject();
//                            detailparmascondition.put("transactCode", TRANSACTCODE);
//                            detailparmascondition.put("type", "condition");
//                            detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmascondition);
//                            String subjectIdsGr = "";
//                            content = this.parseDetailPagecondition(detailResponse, TRANSACTCODE, subjectIdsGr);
//                            detailmessage.put("受理条件", content);
//
//                            List<JSONObject> midobject = new ArrayList<>();
//
//                            JSONObject detailinfoExt = new JSONObject();
//                            detailinfoExt.put("transactCode", TRANSACTCODE);
//                            detailinfoExt.put("type", "infoExt");
//                            detailResponse = postFormReq(detailUrl, new JSONObject(), detailinfoExt);
//                            content = this.parseDetailinfoExt(detailResponse, TRANSACTCODE);
//                            midobject.add(content);
//                            //detailmessage.put("基本信息",content);
//
//                            JSONObject detaildocument = new JSONObject();
//                            detaildocument.put("transactCode", TRANSACTCODE);
//                            detaildocument.put("type", "document");
//                            detailResponse = postFormReq(detailUrl, new JSONObject(), detaildocument);
//                            content = this.parseDetaildocument(detailResponse);
//                            midobject.add(content);
//                            //detailmessage.put("审批结果信息",content);
//
//                            content = this.parseDetailbanlixinxi(TRANSACTCODE);
//                            midobject.add(content);
//                            //detailmessage.put("办理信息",content);
//
//                            content = this.parseDetailqitaxinxi(TRANSACTCODE);
//                            midobject.add(content);
//                            //detailmessage.put("其他信息",content);
//
//                            detailmessage.put("基本信息", midobject);
//
//                            content = this.parseDetailchuankoubanli(TRANSACTCODE);
//                            detailmessage.put("窗口办理", content);
//
//                            content = this.parseDetailshenqingtiaojian(TRANSACTCODE);
//                            detailmessage.put("申请条件", content);
//
//                            content = this.parseDetailxukeshoufei();
//                            detailmessage.put("许可收费", content);
//
//                            content = this.parseDetailzhongjiefuwu(TRANSACTCODE);
//                            detailmessage.put("中介服务", content);

                            //System.out.println(detailmessage);

                        }
                    }

                }
                ++currentPage;
            }

        return null;
    }
    public static void main (String[] args) {
        SpiderHBZWGGFW spiderHBZWGGFW = new SpiderHBZWGGFW();
        spiderHBZWGGFW.getDataDemo();

    }
}
